package com.mineoj.controller;

import com.mineoj.common.BaseResponse;
import com.mineoj.common.ErrorCode;
import com.mineoj.common.ResultUtils;
import com.mineoj.exception.BusinessException;
import com.mineoj.model.dto.userfollow.UserFollowAddRequest;
import com.mineoj.model.entity.User;
import com.mineoj.service.UserFollowService;
import com.mineoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子点赞接口
 *
 
 */
@RestController
@RequestMapping("/user_follow")
@Slf4j
public class UserFollowController {

    @Resource
    private UserFollowService userFollowService;

    @Resource
    private UserService userService;

    /**
     * 关注 / 关注点赞
     *
     * @param userFollowAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doUserFollow(@RequestBody UserFollowAddRequest userFollowAddRequest,
            HttpServletRequest request) {
        if (userFollowAddRequest == null || userFollowAddRequest.getFollowUserId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final User loginUser = userService.getLoginUser(request);
        long followUserId = userFollowAddRequest.getFollowUserId();
        int result = userFollowService.doUserFollow(followUserId, loginUser);
        return ResultUtils.success(result);
    }

}
